<!--
 * @Author: HeAo
 * @Date: 2021-10-12 16:10:17
 * @LastEditTime: 2021-10-12 16:40:02
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2103code\3-2-Vue\day04\6-自定义指令.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id="app">
    <!-- <div v-mydir="msg">指令绑定dom元素</div> -->
    <input type="text" v-mymodel="message">
    {{message}} <br>
    <input type="text" v-mymodel="msg">
    {{msg}}
  </div>
  <script>
    // 1.指令配置项的声明
    let directive1 = {
      bind(el, binding, vnode, oldVnode) {
        console.log('bind', el, binding, vnode, oldVnode);
      },
      inserted(el, binding, vnode, oldVnode) {
        console.log('inserted', el, binding, vnode, oldVnode);
      },
      update(el, binding, vnode, oldVnode) {
        console.log('update', el, binding, vnode, oldVnode);
      },
      componentUpdated(el, binding, vnode, oldVnode) {
        console.log('componentUpdated', el, binding, vnode, oldVnode);
      },
      unbind(el, binding, vnode, oldVnode) {
        console.log('unbind', el, binding, vnode, oldVnode);
      }
    }

    let directive2 = {
      bind(el, binding, vnode, oldVnode) {
        console.log('bind', el, binding, vnode, oldVnode);
        // 双向数据绑定线路一，将数据模型中的数据绑定到目标元素的value上
        el.value = binding.value
        // 双向数据绑定线路二，将视图更新的数据响应到对应数据模型中
        el.oninput = (event) => {
          vnode.context.$root[binding.expression] = event.target.value
        }
      },
      inserted(el, binding, vnode, oldVnode) {
        console.log('inserted', el, binding, vnode, oldVnode);
      },
      update(el, binding, vnode, oldVnode) {
        console.log('update', el, binding, vnode, oldVnode);
      },
      componentUpdated(el, binding, vnode, oldVnode) {
        console.log('componentUpdated', el, binding, vnode, oldVnode);
      },
      unbind(el, binding, vnode, oldVnode) {
        console.log('unbind', el, binding, vnode, oldVnode);
      }
    }
    // 2.全局注册指令
    Vue.directive('mydir', directive1)

    let vm = new Vue({
      el: '#app',
      data: {
        msg: 'hello 指令',
        message: '我是根实例数据'
      },
      methods: {},
      created() { },
      // 2.1局部注册自定义指令
      directives: {
        mymodel: directive2
      }
    })
  </script>
</body>

</html>